home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270_src.lha / gcc-2.7.0-amiga / bi-lexer.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  4KB  |  172 lines

  1. /* Lexer for scanner of bytecode definition file.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "hconfig.h"
  23. #include "bi-parser.h"
  24.  
  25. /* Current read buffer and point */
  26. static char *buffer = NULL;
  27. static char *inpoint = NULL;
  28.  
  29.  
  30. /* Safely allocate NBYTES bytes of memory.  Returns pointer to block of
  31.    memory. */
  32.  
  33. static char *
  34. xmalloc (nbytes)
  35.      int nbytes;
  36. {
  37.   char *tmp = (char *) malloc (nbytes);
  38.  
  39.   if (!tmp)
  40.     {
  41.       fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n", nbytes);
  42.       exit (FATAL_EXIT_CODE);
  43.     }
  44.  
  45.   return tmp;
  46. }
  47.  
  48.  
  49. /* Safely reallocate BLOCK so its size becomes NBYTES.
  50.    The block returned may be different from the one supplied. */
  51.  
  52. static char *
  53. xrealloc (block, nbytes)
  54.      char *block;
  55.      int nbytes;
  56. {
  57.   char *tmp = (block
  58.            ? (char *) realloc (block, nbytes)
  59.            : (char *) malloc (nbytes));
  60.  
  61.   if (!tmp)
  62.     {
  63.       fprintf (stderr, "can't reallocate %d bytes (out of virtual memory)\n", nbytes);
  64.       exit (FATAL_EXIT_CODE);
  65.     }
  66.  
  67.   return tmp;
  68. }
  69.  
  70.  
  71. /* Scan for string token on standard input.  A string is, for our
  72.    purposes here, a sequence of characters that starts with the regexp
  73.    ``[^ #\t\n(),]'' and is then followed by the regexp ``[^#(),]*''. Any
  74.    character is accepted if preceded by a backslash, "\\".  It is assumed
  75.    that the first character has already been checked by the main loop. */
  76.  
  77. static char *
  78. scan_string ()
  79. {
  80.   char *buffer = NULL;
  81.   char *point = NULL;
  82.   int buffer_size = 0;
  83.   int c;
  84.  
  85.   while ((c = getc (stdin)) != EOF
  86.      && c != '#' && c != '(' && c != ')' && c != ',')
  87.     {
  88.       /* Extend buffer, if necessary (minus two so there's room for the NUL
  89.      trailer as well as another character if this one is a backslash).  */
  90.       if (!buffer_size || (point - buffer >= buffer_size-2))
  91.     {
  92.       int previous_point_index = point - buffer;
  93.  
  94.       buffer_size = (!buffer_size ? 32 : buffer_size * 2);
  95.       if (!buffer)
  96.         buffer = xmalloc (buffer_size);
  97.       else
  98.         buffer = xrealloc (buffer, buffer_size);
  99.       
  100.       point = buffer + previous_point_index;
  101.     }
  102.       *point++ = c & 0xff;
  103.  
  104.       if (c == '\\')
  105.     {
  106.       c = getc (stdin);
  107.  
  108.       /* Catch special case: backslash at end of file */
  109.       if (c == EOF)
  110.         break;
  111.  
  112.       *point++ = c;
  113.     }
  114.     }
  115.   *point = 0;
  116.  
  117.   if (c != EOF)
  118.     ungetc (c, stdin);
  119.  
  120.   return buffer;
  121. }
  122.  
  123.  
  124. int
  125. yylex ()
  126. {
  127.   int c;
  128.   char *token;
  129.  
  130.  
  131.   /* First char determines what token we're looking at */
  132.   for (;;)
  133.     {
  134.       c = getc (stdin);
  135.  
  136.       switch (c)
  137.     {
  138.     case EOF:
  139.       return 0;
  140.       
  141.     case ' ':
  142.     case '\t':
  143.     case '\n':
  144.       /* Ignore whitespace */
  145.       continue;
  146.       
  147.     case '#':
  148.       /* Comments advance to next line */
  149.       while ((c = getc (stdin)) != '\n' && c != EOF);
  150.       continue;
  151.       
  152.     default:
  153.       if (c != '(' && c != ')' && c != '\\' && c != ',')
  154.         {
  155.           ungetc (c, stdin);
  156.           yylval.string = scan_string ();
  157.  
  158.           /* Check if string is "define_operator"; if so, return
  159.          a DEFOP token instead.  */
  160.           if (!strcmp (yylval.string, "define_operator"))
  161.         {
  162.           free (yylval.string);
  163.           yylval.string = 0;
  164.           return DEFOP;
  165.         }
  166.           return STRING;
  167.         }
  168.       return c & 0xff;
  169.     }
  170.     }
  171. }
  172.